package com.itheima.collection1;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;

public class Demo2 {

    public static void main(String[] args) {

        //Collection是单列集合的顶层接口，它定义了单列集合最基本的操作方法

        Collection<String> list = new ArrayList<>();

        //1.添加元素：boolean add(E e)
        list.add("hello");
        list.add("world");
        list.add("java");
        System.out.println(list);

        //2.删除元素：boolean remove(Object o)
        list.remove("java");
        System.out.println(list);

        //3.判断元素：boolean contains(Object o)
        boolean b = list.contains("world");
        System.out.println(b);

        //4.判断是否为空：boolean isEmpty()
        boolean b1 = list.isEmpty();
        System.out.println(b1);

        //5.获取元素个数：int size()
        int size = list.size();
        System.out.println(size);

        //6.清空集合：void clear()
        list.clear();
        System.out.println(list);

        //7.把集合转换为数组：Object[] toArray()
        Object[] arr = list.toArray();
        System.out.println(Arrays.toString(arr));

        //把集合转换成字符串数组
        //String[] arr1 = list.toArray(new String[0]);
        String[] arr1 = list.toArray(String[]::new);        //方法引用，此处是引用构造器



    }
}
